package hotel.modal;

import hotel.dao.OrderDao;
import javafx.beans.property.*;

public class Order {
	private SimpleStringProperty orderId;		// 订单号
	private SimpleStringProperty preOrderId;	// 开房单号
	private SimpleDoubleProperty basicCost;		// 开房费用
	private SimpleDoubleProperty otherCost;		// 其他费用
	private SimpleDoubleProperty totalCost;		// 应收金额
	private SimpleDoubleProperty realCharge;	// 实收金额
	private SimpleStringProperty createTime;	// 创建时间
	private SimpleStringProperty roomNo;
	private SimpleStringProperty typeName;
	private SimpleStringProperty startTime;
	private SimpleStringProperty leaveTime;
	private SimpleStringProperty name;
	private SimpleDoubleProperty discount;
	private SimpleBooleanProperty valid;		// 有效标记
	protected SimpleBooleanProperty modified;
	
	private Room room;
	private RoomRent roomRent;
	private Customer customer;
	
	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
		if(room != null) {
			this.roomNo.set(room.getRoomNo());
			this.typeName.set(room.getTypeName() + (room.isHourRoom()?"(钟点房)":""));
		}
	}
	
	public RoomRent getRoomRent() {
		return roomRent;
	}

	public void setRoomRent(RoomRent roomRent) {
		this.roomRent = roomRent;
		if(roomRent != null) {
			this.startTime.set(roomRent.getCreateTime());
			this.leaveTime.set(roomRent.getLeaveTime());
		}
	}
	
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
		if(customer != null) {
			this.name.set(customer.getName());
			this.discount.set(customer.getDiscountRate());
		}
	}

	private void init(OrderDao od) {
		if(od != null) {
			this.orderId = new SimpleStringProperty(od.getOrderId());
			this.preOrderId = new SimpleStringProperty(od.getPreOrderId());
			this.basicCost = new SimpleDoubleProperty(od.getBasicCost());
			this.otherCost = new SimpleDoubleProperty(od.getOtherCost());
			this.totalCost = new SimpleDoubleProperty(od.getTotalCost());
			this.realCharge = new SimpleDoubleProperty(od.getRealCharge());
			this.createTime = new SimpleStringProperty(od.getCreateTime());
			this.valid = new SimpleBooleanProperty(od.isValid());
		}else {
			this.orderId = new SimpleStringProperty("");
			this.preOrderId = new SimpleStringProperty("");
			this.basicCost = new SimpleDoubleProperty(0.0);
			this.otherCost = new SimpleDoubleProperty(0.0);
			this.totalCost = new SimpleDoubleProperty(0.0);
			this.realCharge = new SimpleDoubleProperty(0.0);
			this.createTime = new SimpleStringProperty("");
			this.valid = new SimpleBooleanProperty(true);
		}
		this.roomNo = new SimpleStringProperty("");
		this.typeName = new SimpleStringProperty("");
		this.startTime = new SimpleStringProperty("");
		this.leaveTime = new SimpleStringProperty("");
		this.name = new SimpleStringProperty("");
		this.discount = new SimpleDoubleProperty(1.0);
		this.modified = new SimpleBooleanProperty(false);
	}
	
	public Order(OrderDao od) {
		init(od);
	}
	
	public Order() {
		init(null);
	}
	
	public Object getDao() {
		OrderDao od = new OrderDao();
		od.setOrderId(orderId.get());
		od.setPreOrderId(preOrderId.get());
		od.setBasicCost(basicCost.get());
		od.setOtherCost(otherCost.get());
		od.setTotalCost(totalCost.get());
		od.setRealCharge(realCharge.get());
		od.setCreateTime(createTime.get());
		od.setValid(valid.get());
		return od;
	}
	
	public Order(Order order) {
		this.orderId = new SimpleStringProperty(order.getOrderId());
		this.preOrderId = new SimpleStringProperty(order.getPreOrderId());
		this.basicCost = new SimpleDoubleProperty(order.getBasicCost());
		this.otherCost = new SimpleDoubleProperty(order.getOtherCost());
		this.totalCost = new SimpleDoubleProperty(order.getTotalCost());
		this.realCharge = new SimpleDoubleProperty(order.getRealCharge());
		this.createTime = new SimpleStringProperty(order.getCreateTime());
		this.valid = new SimpleBooleanProperty(order.isValid());
		this.modified = new SimpleBooleanProperty(false);
		this.roomNo = new SimpleStringProperty("");
		this.typeName = new SimpleStringProperty("");
		this.startTime = new SimpleStringProperty("");
		this.leaveTime = new SimpleStringProperty("");
		this.name = new SimpleStringProperty("");
		this.discount = new SimpleDoubleProperty(1.0);
		setRoom(order.getRoom());
		setCustomer(order.getCustomer());
		setRoomRent(order.getRoomRent());
	}
	
	public Order(String orderId, String preOrderId, double basicCost, double otherCost, 
			double totalCost, double realCharge, String createTime, int valid) {
		this.orderId = new SimpleStringProperty(orderId);
		this.preOrderId = new SimpleStringProperty(preOrderId);
		this.basicCost = new SimpleDoubleProperty(basicCost);
		this.otherCost = new SimpleDoubleProperty(otherCost);
		this.totalCost = new SimpleDoubleProperty(totalCost);
		this.realCharge = new SimpleDoubleProperty(realCharge);
		this.createTime = new SimpleStringProperty(createTime);
		if(valid > 0) this.valid = new SimpleBooleanProperty(true);
		else this.valid = new SimpleBooleanProperty(false);
		this.modified = new SimpleBooleanProperty(false);
	}
	
	
	@Override
	public String toString() {
		String str = String.format("OrderId:%s PreOrderId:%s BasicCost:%.2f OtherCost:%.2f TotalCost:%.2f "+
				"RealCharge:%.2f CreateTime:%s Valid:%d",
				orderId.get(), preOrderId.get(), basicCost.get(), otherCost.get(), totalCost.get(),
				realCharge.get(), createTime.get(), valid.get());
		return str;
	}
	
	public final SimpleStringProperty orderIdProperty() {
		return this.orderId;
	}
	
	public final String getOrderId() {
		return this.orderIdProperty().get();
	}
	
	public final void setOrderId(final String orderId) {
		this.orderIdProperty().set(orderId);
	}
	
	public final SimpleStringProperty preOrderIdProperty() {
		return this.preOrderId;
	}
	
	public final String getPreOrderId() {
		return this.preOrderIdProperty().get();
	}
	
	public final void setPreOrderId(final String preOrderId) {
		this.preOrderIdProperty().set(preOrderId);
	}
	
	public final SimpleDoubleProperty basicCostProperty() {
		return this.basicCost;
	}
	
	public final double getBasicCost() {
		return this.basicCostProperty().get();
	}
	
	public final void setBasicCost(final double basicCost) {
		this.basicCostProperty().set(basicCost);
	}
	
	public final SimpleDoubleProperty otherCostProperty() {
		return this.otherCost;
	}
	
	public final double getOtherCost() {
		return this.otherCostProperty().get();
	}
	
	public final void setOtherCost(final double otherCost) {
		this.otherCostProperty().set(otherCost);
	}
	
	public final SimpleDoubleProperty totalCostProperty() {
		return this.totalCost;
	}
	
	public final double getTotalCost() {
		return this.totalCostProperty().get();
	}
	
	public final void setTotalCost(final double totalCost) {
		this.totalCostProperty().set(totalCost);
	}
	
	public final SimpleDoubleProperty realChargeProperty() {
		return this.realCharge;
	}
	
	public final double getRealCharge() {
		return this.realChargeProperty().get();
	}
	
	public final void setRealCharge(final double realCharge) {
		this.realChargeProperty().set(realCharge);
	}
	
	public final SimpleStringProperty createTimeProperty() {
		return this.createTime;
	}
	
	public final String getCreateTime() {
		return this.createTimeProperty().get();
	}
	
	public final void setCreateTime(final String createTime) {
		this.createTimeProperty().set(createTime);
	}
	
	public final SimpleBooleanProperty validProperty() {
		return this.valid;
	}
	
	public final boolean isValid() {
		return this.validProperty().get();
	}
	
	public final void setValid(final boolean valid) {
		this.validProperty().set(valid);
	}

	public final SimpleBooleanProperty modifiedProperty() {
		return this.modified;
	}
	

	public final boolean isModified() {
		return this.modifiedProperty().get();
	}
	

	public final void setModified(final boolean modified) {
		this.modifiedProperty().set(modified);
	}

	public final SimpleStringProperty roomNoProperty() {
		return this.roomNo;
	}
	

	public final String getRoomNo() {
		return this.roomNoProperty().get();
	}
	

	public final void setRoomNo(final String roomNo) {
		this.roomNoProperty().set(roomNo);
	}	

	public final SimpleStringProperty startTimeProperty() {
		return this.startTime;
	}
	

	public final String getStartTime() {
		return this.startTimeProperty().get();
	}
	

	public final void setStartTime(final String startTime) {
		this.startTimeProperty().set(startTime);
	}
	

	public final SimpleStringProperty leaveTimeProperty() {
		return this.leaveTime;
	}
	

	public final String getLeaveTime() {
		return this.leaveTimeProperty().get();
	}
	

	public final void setLeaveTime(final String leaveTime) {
		this.leaveTimeProperty().set(leaveTime);
	}
	

	public final SimpleDoubleProperty discountProperty() {
		return this.discount;
	}
	

	public final double getDiscount() {
		return this.discountProperty().get();
	}
	

	public final void setDiscount(final double discount) {
		this.discountProperty().set(discount);
	}

	public final SimpleStringProperty nameProperty() {
		return this.name;
	}
	

	public final String getName() {
		return this.nameProperty().get();
	}
	

	public final void setName(final String name) {
		this.nameProperty().set(name);
	}

	public final SimpleStringProperty typeNameProperty() {
		return this.typeName;
	}
	

	public final String getTypeName() {
		return this.typeNameProperty().get();
	}
	

	public final void setTypeName(final String typeName) {
		this.typeNameProperty().set(typeName);
	}
	
	
}
